<template>
	<view >
		<block>
			<uni-nav-bar @clickLeft="goBack" status-bar="true" shadow="true" left-icon="back" left-text="返回" title="权限申请"></uni-nav-bar>
		</block>
		<button style="position: absolute; top: 40%; right: 10px; left: 10px;" class="button" type="primary"  @getuserinfo="onGotUserInfo" open-type="getUserInfo" >获取用户信息权限</button>
		<uni-popup ref="showtip" :type="type" :mask-click="false" @change="change">
			<view class="uni-tip">
				<text class="uni-tip-title">警告</text>
				<text class="uni-tip-content">需要通过授权才能继续，请重新点击并授权！</text>
				<view class="uni-tip-group-button">
					<text class="uni-tip-button" @click="cancel('tip')">取消</text>
					<text class="uni-tip-button" @click="cancel('tip')">确定</text>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import config from "@/common/config.js"
	export default {
		components: {
			uniNavBar,
			uniPopup
		},
		data() {
			return {
				type: '',
				list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
				content: '顶部弹 popup',
				goto:''
			}
		},
		onLoad(option) {
			console.log('99999999999'+option.goto);
			this.goto = option.goto;
		},
		methods: {
			goBack() {
				uni.navigateBack({

				})
			},
			cancel(type) {
				this.$refs['show' + type].close();
			},
			change(e) {
				console.log('是否打开:' + e.show);
			},
			onGotUserInfo(e) {
				var userInfo = e.detail.userInfo;
				if (userInfo) {
					console.log('授权成功');
					//授权成功后第一时间去绑定账号()
					this.toLoginWeixinAndGetSessionkey(userInfo);
				} else {
					console.log('授权被拒');
					this.$nextTick(() => {
						this.type = 'center'
						this.$refs['show' + 'tip'].open()
					})
				}
			},
			toLoginWeixinAndGetSessionkey(userInfo) {
				var info = userInfo;

				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						// console.log(loginRes.code);
						//拿到code访问业务后台，业务后台访问微信平台，获得openId
						var b = config.baseUrl + '/thirdbind/weixinsessionkey';
						uni.request({
							method: 'POST',
							url: b,
							data: {
								code: loginRes.code, //服务端直接用ctx.request.body.code 接收
								userInfo: info
							},
							header: {
								'content-type': 'application/json',
								'Authorization': config.token //通过登录接口，生成新的token
							},
							success: (res) => {
								var statusCode = res.statusCode;
								console.log(res);
								if (200 == statusCode) {
									//将微信平台返回的session_key保存到本地，为了让业务后台方便查询到对应到openId，进而判断是否已绑定user库
									//在onLoad()方法中检查是否过期
									console.log(res.data);
									uni.setStorageSync(config.sessionkey, res.data);
									console.log('成功获得sesiongkey');
									if(this.goto){
										uni.navigateTo({
											url: '../loginbytelephone/loginbytelephone'
										})
									}else{
										uni.navigateTo({
											url: '../loginnew/loginnew'
										})
									}
									
								} else {
									console.log('失败获得sesiongkey');
								}
							},
							fail: (res) => {
								console.log('失败获得sesiongkey');
							}
						});

					}
				});
			}
		}
	}
</script>

<style>
	

	.uni-tip {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: column;
		/* #endif */
		padding: 15px;
		width: 300px;
		background-color: #fff;
		border-radius: 10px;
	}

	.uni-tip-title {
		margin-bottom: 10px;
		text-align: center;
		font-weight: bold;
		font-size: 16px;
		color: #333;
	}

	.uni-tip-content {
		/* padding: 15px;
 */
		font-size: 14px;
		color: #666;
	}

	.uni-tip-group-button {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		margin-top: 20px;
	}

	.uni-tip-button {
		flex: 1;
		text-align: center;
		font-size: 14px;
		color: #3b4144;
	}

	/* 插屏广告 */
	.uni-image {
		position: relative;
	}

	.image {
		width: 200px;
		height: 200px;
	}

	.uni-image-close {
		margin-top: 20px;
		text-align: center;
	}

	/* 底部分享 */
	.uni-share {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: column;
		/* #endif */
		background-color: #fff;
	}

	.uni-share-title {
		line-height: 60rpx;
		font-size: 24rpx;
		padding: 15rpx 0;
		text-align: center;
	}

	.uni-share-content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: center;
		padding: 15px;
	}

	.uni-share-content-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		width: 200rpx;
	}

	.uni-share-content-image {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		width: 60rpx;
		height: 60rpx;
		overflow: hidden;
		border-radius: 10rpx;
	}

	.content-image {
		width: 60rpx;
		height: 60rpx;
	}

	.uni-share-content-text {
		font-size: 26rpx;
		color: #333;
		padding-top: 5px;
		padding-bottom: 10px;
	}

	.uni-share-btn {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 14px;
		border-top-color: #f5f5f5;
		border-top-width: 1px;
		border-top-style: solid;
		text-align: center;
		color: #666;
	}
</style>
